public class MyLinkedList {
    static class ListNode{
        int val;
        ListNode prev;
        ListNode next;

        ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode tail;

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
            this.tail = node;
        } else {
            this.head.prev = node;
            node.next = this.head;
            this.head = node;
        }

    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (this.head == null) {
            this.head = node;
            this.tail = node;
        } else {
            this.tail.next = node;
            node.prev = this.tail;
            this.tail = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        // 判断下标合法性
        if (index < 0 || index > size()) {
            System.out.println("坐标不合法！");
            throw new IndexWrongFulException("坐标不合法！");
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);

        // 判断特殊位置
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }

        node.prev = cur.prev;
        node.next = cur;
        cur.prev = node;
        node.prev.next = node;
    }
    private ListNode findIndex(int index) {
        ListNode cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                // 判断要删除的节点是否在头
                if (cur == this.head) {
                    this.head = this.head.next;

                    // 判断新头是否为空，如果为空，设置尾也为空，如果不为空，将头前驱置空
                    if (this.head != null) {
                        this.head.prev = null;
                    } else {
                        this.tail = null;
                    }
                } else {
                    cur.prev.next = cur.next;
                    // 判断是不是在尾巴
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        this.tail = cur.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                // 判断要删除的节点是否在头
                if (cur == this.head) {
                    this.head = this.head.next;

                    // 判断新头是否为空，如果为空，设置尾也为空，如果不为空，将头前驱置空
                    if (this.head != null) {
                        this.head.prev = null;
                    } else {
                        this.tail = null;
                    }
                } else {
                    cur.prev.next = cur.next;
                    // 判断是不是在尾巴
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        this.tail = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
        this.tail = null;
    }
}
